package com.settlement.system.controller;

import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.TblQueryTableConfig;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblQueryFieldConfigService;
import com.settlement.system.service.ITblQueryTableConfigService;
import com.alibaba.excel.context.AnalysisContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 联表查询表配置表 前端控制器
 * @author xiaochuan
 * @since 2022-01-18
 */

@Api(tags = "联表查询表配置表",description = "联表查询表配置表")
@RestController
@RequestMapping("/tblQueryTableConfig")
public class TblQueryTableConfigController extends ExcelController<TblQueryTableConfig> {

    @Resource
    private ITblQueryTableConfigService tblQueryTableConfigService;

    @Resource
    private ITblQueryFieldConfigService tblQueryFieldConfigService;

    @Override
    public IBaseService init() {
        return tblQueryTableConfigService;
    }

    @Override
    public Class<?> getModelClass() {
        return TblQueryTableConfig.class;
    }

    @Override
    public String verifyImportObject(TblQueryTableConfig tblQueryTableConfig, AnalysisContext analysisContext) {
        String message = super.verifyImportObject(tblQueryTableConfig,analysisContext);
        if(StringUtils.isNotEmpty(message)){
            return message;
        }
        try {
            Map map = new HashMap<>();
            map.put("tableNameZn",tblQueryTableConfig.getTableNameZn());
            List<TblQueryTableConfig> tblQueryTableConfigs = tblQueryTableConfigService.selectByParam(map);
            if(!CollectionUtils.isEmpty(tblQueryTableConfigs)){
                return "数据已存在";
            }

            String tableNameCn = tblQueryTableConfigService.getTableNameCnByTableNameZn(tblQueryTableConfig.getTableNameZn());
            tblQueryTableConfig.setTableNameCn(tableNameCn);
        } catch (ServiceException e) {
            e.printStackTrace();
            return "校验时查询出错";
        }

        return null;
    }

    @Override
    protected void saveData(List<TblQueryTableConfig> datas) throws ServiceException {

        List<String> list = new ArrayList<>();
        for(TblQueryTableConfig data : datas){
            String toString = data.toString();
            if(list.contains(toString)){
                throw new ServiceException("有两行数据重复");
            }else{
                list.add(toString);
            }
        }

        super.saveData(datas);
        for (TblQueryTableConfig data : datas) {
            Map<String, Object> map = new HashMap<>();
            map.put("tableNameZn",data.getTableNameZn());
            tblQueryFieldConfigService.refresh(map);
        }
    }


    @ApiOperation("获取表名列表")
    @RequestMapping(value = "pullTableNameList", method = {RequestMethod.POST})
    @ResponseBody
    protected ObjectDataResponse pullTableNameList(HttpServletRequest request, @RequestBody Map<String, Object> model) {
        return tblQueryTableConfigService.pullTableNameList(model);
    }


}

